import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
//1.引入element-plus组件库
import ElementPlus, {ElMessage} from 'element-plus'
import 'element-plus/dist/index.css'
//3.引入element-plus图标库
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
//5.1修改elementPlus默认中文
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import axios from "axios";


const BASE_URL = 'http://localhost:8080';
//window里配置全局内容,可以在任意script标签中使用
window.BASE_URL = BASE_URL;

const app = createApp(App);
//8.2给app也就是vue实例配置全局根路径,方便在template中使用
app.config.globalProperties.BASE_URL = BASE_URL;

app.config.globalProperties.flexColumnWidth = (str, arr1, flag = 'max') => {
    // flag为'max'则设置列宽适配该列中最长的内容,flag为'equal'则设置列宽适配该列中第一行内容的长度。
    str = str + ''
    let columnContent = ''
    if (!arr1 || !arr1.length || arr1.length === 0 || arr1 === undefined) {
        return
    }
    if (!str || !str.length || str.length === 0 || str === undefined) {
        return
    }

    // 获取该列中最长的数据(内容)
    let index = 0
    for (let i = 0; i < arr1.length; i++) {
        try {
            if (arr1[i][str] === null) {
                continue
            }
        } catch (error) {
            console.warn(arr1[i],error)
            continue
        }
        const now_temp = arr1[i][str] + ''
        const max_temp = arr1[index][str] + ''
        if (now_temp.length > max_temp.length) {
            index = i
        }
    }
    columnContent = arr1[index][str]

    // console.log('该列数据[i]:', columnContent)
    // 以下分配的单位长度可根据实际需求进行调整
    let flexWidth = 0
    for (const char of columnContent) {
        if ((char >= 'A' && char <= 'Z') || (char >= 'a' && char <= 'z')) {
            // 如果是英文字符
            flexWidth += 10
        } else if (char >= '\u4e00' && char <= '\u9fa5') {
            // 如果是中文字符
            flexWidth += 18
        } else if (char >= '0' && char <= '9') {
            // 如果是数字
            flexWidth += 12
        } else {
            // 其他种类字符
            flexWidth += 10
        }
    }
    if (flexWidth < 100) {
        // 设置最小宽度
        flexWidth = 100
    }
    // if (flexWidth > 250) {
    //   // 设置最大宽度
    //   flexWidth = 250
    // }
    return flexWidth + 'px'
}

//4.遍历ElementPlusIconsVue对象中的所有图标组件并注册到app中
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}

//2.app.use(ElementPlus) 使用Element-plus组件库
//5.2 app.use(ElementPlus,{ locale: zhCn })将El本地化为中文
app.use(ElementPlus,{ locale: zhCn }).use(store).use(router).mount('#app')

//6.解决ResizeObserver Error
const debounce = (fn, delay) => {
    let timer = null;
    return function () {
        let context = this;
        let args = arguments;
        clearTimeout(timer);
        timer = setTimeout(function () {
            fn.apply(context, args);
        }, delay);
    }
}
const _ResizeObserver = window.ResizeObserver;
window.ResizeObserver = class ResizeObserver extends _ResizeObserver {
    constructor(callback) {
        callback = debounce(callback, 16);
        super(callback);
    }
}

window.getUser = ()=>{
    //如果获取到了用户数据,则转回JS对象
    //如果没有获取到用户数据,转为布尔是false,值为null
    return localStorage.user ? JSON.parse(localStorage.user) : null;
}

window.loadData = (object, type)=>{
    axios.get(BASE_URL+'/hhmc/' + type + '/list/').then((response)=>{
        if(response.data.code==2000){
            object.value = response.data.data;
            // console.log(object.value);
        }else{
            ElMessage.error(response.data.msg);
        }
    })
}